package com.itheima.admin.controller;

import com.itheima.admin.dto.AdChannelDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import com.itheima.core.controller.AbstractCoreController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(tags = "频道管理")
@RestController
@RequestMapping("/channel")
public class AdChannelController extends AbstractCoreController<AdChannel> {
    private AdChannelService adChannelService;

    public AdChannelController(AdChannelService adChannelService) {
        super(adChannelService);
        this.adChannelService=adChannelService;
    }
    /*@Autowired
    private AdChannelService adChannelService;*/

    /**
     * 分页查询
     * @param adChannelDto  接收前端传过来的参数
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("频道分页查询接口")
    //接受前端页面传过来的json数据
    public PageResultVo findList(@RequestBody AdChannelDto adChannelDto){
        //调用Service层的方法进行分页查询并返回
        return adChannelService.findList(adChannelDto);
    }

    /**
     *新增频道的接口
     * @param adChannel 接收前端传来的新增频道数据
     * @return
     *//*
    @ApiOperation("新增频道接口")
    @PostMapping("/save")
    public ResultVo add(@RequestBody AdChannel adChannel){
        //判断参数是否为空，为空返回参数不正确
        if (adChannel==null){
            return ResultVo.error(HttpCodeEnum.DATA_NOT_EXIST);
        }
        adChannel.setStatus(false);
        adChannel.setCreatedTime(new Date());
        //调用service进行查询是否添加成功返回相应的参数
        if (adChannelService.save(adChannel)){
            return ResultVo.ok("添加成功");
        }
        return ResultVo.error();
    }

    *//**
     * 根据频道id进行查询
     * @param id 接收前端传过来的id
     * @return
     *//*
    @ApiOperation("根据id查询频道")
    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable Long id){
        return ResultVo.ok(adChannelService.getById(id));
    }

    *//**
     * 根据频道id删除频道，返回相应的数据
     * @param id  接收前端传过来的id
     * @return
     *//*
    @ApiOperation("删除数据接口")
    @DeleteMapping("/del/{id}")
    public ResultVo delete(@PathVariable Long id){
        if (adChannelService.removeById(id)){
            return ResultVo.ok("删除频道成功!");
        }
        return ResultVo.error();
    }

    *//**
     * 根据前端传过来的参数来修改频道
     * @param adChannel 接收前端传过来的参数
     * @return
     *//*
    //@ApiOperation("跟新频道接口")
    @ApiOperation("更新频道接口")
    @PostMapping("/update")
    public ResultVo update(@RequestBody AdChannel adChannel){
        if (adChannel.getId()==null){
            return ResultVo.error(HttpCodeEnum.PARAM_REQUIRE);
        }
        //调用service进行更新，判断是否更新成功返回相应的参数
        if (adChannelService.updateById(adChannel)){
            return ResultVo.ok("更新频道成功！");
        }
        return ResultVo.error();

    }
    @DeleteMapping("/xxx")
    public ResultVo xxx(){
        try {
            System.out.println(1/0);
        }catch (Exception e){
            throw new LeadNewsException("你要删除的东西不存在");
        }
        return ResultVo.ok();
    }*/
}
